package com.firewolf.springcloud.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.firewolf.springcloud.entities.Dept;

@RestController
public class DeptController {

	Logger logger = LoggerFactory.getLogger(DeptController.class);

	// 这里不会再自己写服务，而是通过rest调用provider的服务
	@Autowired
	private RestTemplate template;
	private final String REST_URL_PREFIX = "http://localhost:8001";//provider的地址

//	@PostMapping("add") 
	@RequestMapping("add") //地址栏不能模拟post请求，所以换成@RequestMapping
	public int addDept(Dept d) {
		return template.postForObject(REST_URL_PREFIX+"/add", d, Integer.class);
	}

	@SuppressWarnings("unchecked")
	@GetMapping("list")
	public List<Dept> list() {
		return template.getForObject(REST_URL_PREFIX+"/list", List.class);
	}

	@GetMapping("find/{id}")
	public Dept findByID(@PathVariable("id") Integer id) {
		return template.getForObject(REST_URL_PREFIX+"/find/"+id, Dept.class);
	}

//	@DeleteMapping("del/{id}")
	@RequestMapping("del/{id}") ////地址栏不能模拟delete请求，所以换成@RequestMapping
	public int deleteDept(@PathVariable("id") Integer id) {
		template.delete(REST_URL_PREFIX+"/del/"+id);
		return 1;
	}
}
